package com.student.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.student.demo.config.CommonPage;
import com.student.demo.dto.StudentInfoDTO;
import com.student.demo.config.Result;
import com.student.demo.entity.StudentInfo;
import com.student.demo.mapper.StudentInfoMapper;
import com.student.demo.service.IStudentInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class StudentInfoService extends ServiceImpl<StudentInfoMapper, StudentInfo> implements IStudentInfoService {

    @Transactional
    @Override
    public Result<?> insert(StudentInfo studentInfo) {
        save(studentInfo);
        return Result.success();
    }

    @Transactional
    @Override
    public Result<?> delInfo(Long id) {
        try {
            StudentInfo byId = getById(id);
            LambdaUpdateChainWrapper<StudentInfo> updateWrapper = new LambdaUpdateChainWrapper<>(baseMapper);
            updateWrapper.eq(StudentInfo::getId, id)
                    .set(StudentInfo::getIsDelete, 1)
                    .update();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
        return Result.success();
    }

    @Transactional
    @Override
    public Result<?> updateInfo(StudentInfo studentInfo) {
        try {
            StudentInfo byId = getById(studentInfo.getId());
            LambdaUpdateChainWrapper<StudentInfo> updateWrapper = new LambdaUpdateChainWrapper<>(baseMapper);
            updateWrapper.eq(StudentInfo::getId, studentInfo.getId()).update(studentInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
        return Result.success();
    }

    @Override
    public Result<?> listPage(StudentInfoDTO studentInfoDTO) {
        // 设置页数和行数
        PageHelper.startPage(studentInfoDTO.getPageNum(), studentInfoDTO.getPageSize());
        QueryWrapper<StudentInfo> queryWrapper = new QueryWrapper<>();
        // 查询姓名
        String stuName = studentInfoDTO.getStuName();
        if (StringUtils.isNotBlank(stuName)){
            queryWrapper.lambda().eq(StudentInfo::getStuName, stuName);
        }
        // 查询学号
        String stuId = studentInfoDTO.getStuId();
        if (StringUtils.isNotBlank(stuId)){
            queryWrapper.lambda().eq(StudentInfo::getStuId, stuId);
        }
        // 查询性别
        Integer stuSex = studentInfoDTO.getStuSex();
        if (stuSex!=null){
            queryWrapper.lambda().eq(StudentInfo::getStuSex, stuSex);
        }
        // 逻辑删除查询
        Integer isDelete = studentInfoDTO.getIsDelete();
        if (isDelete==0){
            queryWrapper.lambda().eq(StudentInfo::getIsDelete, isDelete);
        }
        // 分页
        List<StudentInfo> list = list(queryWrapper);
        CommonPage<StudentInfo> pageInfo = new CommonPage<>().restPage(list);
        return Result.success(pageInfo);
    }


}
